import 'package:file_transfer/fragment/left_drawer_fragment.dart';
import 'package:file_transfer/fragment/top_drawer_fragment.dart';
import 'package:file_transfer/fragment/transfer_fragment.dart';
import 'package:file_transfer/layout/top_sliding_layout.dart';
import 'package:file_transfer/model/message_model.dart';
import 'package:file_transfer/model/simple_model.dart';
import 'package:file_transfer/pojo/message.dart';
import 'package:file_transfer/utils/svg_util.dart';
import 'package:file_transfer/utils/toast_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';


class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return TopSlidingLayout(
      panelHeight: 300,
      slide: TopDrawerFragment(),
      main: Scaffold(
        resizeToAvoidBottomInset:false, // 禁止页面上移
        drawer: LeftDrawerFragment(),
        body: MainLayout(),
      ),
    );
  }
}

class MainLayout extends StatefulWidget {
  const MainLayout({super.key});

  @override
  State<MainLayout> createState() => _MainLayoutState();
}

class _MainLayoutState extends State<MainLayout> {

  _openTopDrawer(){
    final simpleModel = context.read<SimpleModel>();
    final messageModel = context.read<MessageModel>();
    if(!simpleModel.isLogin){
      ToastUtil.show("用户未登录，请先登录！！",textColor: Colors.yellow);
      return;
    }
    if(messageModel.onlineUserList.isEmpty){
      ToastUtil.show("当前没有在线用户！！");
      return;
    }
    TopSlidingLayout.of(context).togglePanel();
  }

  @override
  Widget build(BuildContext context) {
    final messageModel = context.watch<MessageModel>();
    var name = "未选择用户";
    if(messageModel.selectAllClient){
      name = "所有用户";
    }else if(messageModel.client.name == ""){
      final t = messageModel.onlineUserList.firstWhere((e)=>e.key == messageModel.client.key,orElse: () => User(key: ""));
      if(t.key != ""){
        name = t.name;
      }
    }else{
      name = messageModel.client.name;
    }
    return Container(
      color: const Color(0xfff4f4f4),
      child: Column(
        children: [
          Container(
            margin: const EdgeInsets.only(left: 10,right: 10),
            alignment: Alignment.bottomCenter,
            height: 80,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                InkWell(child: SvgPicture.asset(SvgUtil.menuSvg,width: 40,height: 40,),
                  onTap: ()=>Scaffold.of(context).openDrawer()),
                InkWell(
                  onTap: ()=>_openTopDrawer(),
                  child: Text(name,style: const TextStyle(
                    fontSize: 28,
                    fontWeight: FontWeight.bold,
                  ),),
                ),
                const SizedBox(width: 40)
              ],
            ),
          ),
          const Expanded(child: SelectionArea(child: TransferFragment()))
        ],
      ),
    );
  }
}
